package com.sakura.mall.product.attrGroup.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.product.attrGroup.domain.entity.AttrGroupEntity;
import com.sakura.mall.product.attrGroup.domain.request.AddAttrGroupRequest;
import com.sakura.mall.product.attrGroup.domain.request.UpdateAttrGroupRequest;
import com.sakura.mall.product.attrGroup.domain.request.AttrGroupPageRequest;
import com.sakura.mall.product.attrGroup.domain.vo.AttrGroupVo;
import com.sakura.mall.product.attrGroup.mapper.AttrGroupMapper;
import com.sakura.mall.product.attrGroup.service.AttrGroupService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 属性分组 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddAttrGroupRequest request) {
        // todo 其他校验逻辑
        AttrGroupEntity attrGroup = request.convert(AttrGroupEntity.class);
        this.save(attrGroup);
    }

    @Override
    public Page<AttrGroupVo> queryPage(AttrGroupPageRequest request) {
        IPage<AttrGroupEntity> qp = request.toPage();
        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<AttrGroupVo> ret = qp.convert(entity -> entity.convert(AttrGroupVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateAttrGroupRequest request) {
        AttrGroupEntity entity = request.convert(AttrGroupEntity.class);
        entity.setAttrGroupId(id);
        this.updateById(entity);
    }

}

